package com.shanzmoo.core.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.shanzmoo.core.domain.alisms.AliSmsTemplate;
import com.shanzmoo.core.service.IAliSmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.swing.text.html.FormSubmitEvent;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 阿里云 短信服务接口
 *
 * @author Gzhao 2020/12/26
 */
@Slf4j
@Service
public class AliSmsServiceImpl implements IAliSmsService {

    private static final String accessKeyId = "LTAI4GJ8Es6uPCV1AfnkpimQ";
    private static final String accessSecret = "w3UzLMcz1CzBKHxeUlw0k4MZsBJFdr";
    private static final String REGION_ID = "cn-hangzhou";
    private static final String SYS_DOMAIN = "dysmsapi.aliyuncs.com";
    private static final String SYS_VERSION = "2017-05-25";
    private static final String SYS_ACTION = "SendSms";
    private static final String SIGN_NAME = "山之木科技";

    private static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 10, 30L,
            TimeUnit.SECONDS, new ArrayBlockingQueue<>(2000));


    @Override
    public void sendRegisterSms(String phone, String code) {
        threadPoolExecutor.execute(()->{
            sendSms(AliSmsTemplate.REGISTER, phone, code);
        });
    }

    @Override
    public void sendLoginSms(String phone, String code) {
        threadPoolExecutor.execute(()->{
            sendSms(AliSmsTemplate.LOGIN, phone, code);
        });
    }

    private void sendSms(String template, String phone, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(SYS_DOMAIN);
        request.setSysVersion(SYS_VERSION);
        request.setSysAction(SYS_ACTION);
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", template);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", code);

        request.putQueryParameter("TemplateParam", jsonObject.toJSONString());

        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("短信发送，返回结果：{}", response.getData());

        } catch (ServerException e) {

            log.error("短信服务端异常", e);
        } catch (ClientException e) {

            log.error("短信客户端异常", e);
        }
    }
}
